package leecode;

/**
 * @Classname digui
 * @Description TODO
 * @Date 2021/7/20 22:09
 * @Created by Alberthch
 */
class Fibonacci {
    // n只能是正整数或者是0
    public static int fibonacciRecursive(int n) {
        if (n>1) {
            return fibonacciRecursive(n-1)+fibonacciRecursive(n-2);
        } else {
            return n;
        }
    }

    public static int fibonacciIterative(int n) {

        if (n == 1 || n == 0)
            return n;

        int fib0 = 0;
        int fib1 = 1;
        int fib = 0;
        for (int i = 2; i <= n; i++) {
            fib = fib0 + fib1;
            fib0 = fib1;
            fib1 = fib;
        }
        return fib;

    }


    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.print(fibonacciRecursive(i));
            System.out.print(" ");
        }

        System.out.println();
        for (int i = 0; i < 10; i++) {
            System.out.print(fibonacciIterative(i));
            System.out.print(" ");
        }
    }
}
